package com.yrline.pay.services;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.yrline.pay.proxy.request.UserParam;
import com.yrline.pay.proxy.response.AutoSignResponse;
import com.yrline.pay.proxy.response.UserResponse;
import com.yrline.pay.services.vo.UserVo;

@Service
public class UserServices {

	@Autowired
	private StringRedisTemplate redisCached;

	private static String person_key = "USER_PERSON";
	private static String comp_key = "USER_COMPANY";
	private static String mer_key = "USER_MERI";

	public void saveUser(UserParam up) {
		// if(up.getUserType()==1){
		// redisCached.opsForHash().put(person_key, up.getMerBillNo(), up);
		// }else{
		// redisCached.opsForHash().put(comp_key, up.getMerBillNo(), up);
		// }
		redisCached.opsForHash().put(mer_key, up.getMerBillNo(), up);

	}

	public void completeUser(UserResponse response) {
		if (response.getStatus() != 1) {
			return;
		}
		UserParam up = (UserParam) redisCached.opsForHash().get(mer_key,
				response.getMerBillNo());
		UserVo _u = new UserVo();
		BeanUtils.copyProperties(up, _u);
		_u.setIpsAcctNo(response.getIpsAcctNo());
		_u.setStatus(response.getStatus());

		if (up.getUserType() == 1) {
			redisCached.opsForHash().put(person_key, _u.getIpsAcctNo(), _u);
		} else {
			redisCached.opsForHash().put(comp_key, _u.getIpsAcctNo(), _u);
		}
	}

	public List<UserVo> selectUser(Integer type) {
		if (type == null) {
			List _result = redisCached.opsForHash().values(person_key);
			_result.addAll(redisCached.opsForHash().values(comp_key));
			return (List<UserVo>) _result;
		} else if (type == 1) {
			return (List<UserVo>) (List) redisCached.opsForHash().values(
					person_key);
		} else {
			return (List<UserVo>) (List) (redisCached.opsForHash()
					.values(comp_key));
		}
	}

	public UserVo selectUserOne(String ipsAccountNo, Integer type) {
		if (type == 1) {
			return (UserVo) redisCached.opsForHash().get(person_key, ipsAccountNo);
		} else {
			return (UserVo) redisCached.opsForHash().get(comp_key, ipsAccountNo);
		}
	}

	public void addUserMoney(String ipsAccountNo, Integer type, BigDecimal amount,Integer depositType) {
		UserVo _u = selectUserOne(ipsAccountNo, type);
		if (_u != null) {
			BigDecimal camount = _u.getAmount() == null ? BigDecimal.ZERO : _u
					.getAmount();
			if (amount != null) {
				_u.setAmount(camount.add(amount));
				if(depositType !=null && depositType == 2){
					BigDecimal repaymentBal = _u.getRepaymentBal() == null ? BigDecimal.ZERO : _u
							.getRepaymentBal();
					_u.setRepaymentBal(repaymentBal.add(amount));
				}
				this.redisCached.opsForHash().put(getUserKey(type), ipsAccountNo, _u);
			}
		}
	}
	
	public void substringFreeMoney(String ipsAccountNo, Integer type, BigDecimal amount){
		UserVo _u = selectUserOne(ipsAccountNo, type);
		if (_u != null) {
			BigDecimal famount = _u.getFreezeBal() == null ? BigDecimal.ZERO : _u
					.getFreezeBal();
			if (famount != null) {
				_u.setFreezeBal(famount.subtract(amount));
				this.redisCached.opsForHash().put(getUserKey(type), ipsAccountNo, _u);
			}
		}
	}
	
	public void freezeUserMoney(String ipsAccountNo, Integer type, BigDecimal amount) {
		UserVo _u = selectUserOne(ipsAccountNo, type);
		if (_u != null) {
			BigDecimal camount = _u.getAmount() == null ? BigDecimal.ZERO : _u
					.getAmount();
			BigDecimal famount = _u.getFreezeBal() == null ? BigDecimal.ZERO : _u
					.getFreezeBal();
			if (amount != null) {
				_u.setAmount(camount.subtract(amount));
				_u.setFreezeBal(famount.add(amount));
				this.redisCached.opsForHash().put(getUserKey(type), ipsAccountNo, _u);
			}
		}
	}
	
	
	public void autoSignCallback(AutoSignResponse response){
		UserVo u =this.selectUserOne(response.getIpsAcctNo(), response.getUserType());
		if(response.getStatus()==1){
			u.setAutoSignFlag(true);
			u.setIpsAuthNo(response.getIpsAuthNo());
			this.redisCached.opsForHash().put(getUserKey(response.getUserType()), u.getIpsAcctNo(), u);
		}
	}

	private String getUserKey(Integer userType) {
		if (userType == 1) {
			return person_key;
		} else {
			return comp_key;

		}
	}
}
